package com.xinsoft.controller.basic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.aop.ano.SysLog;
import com.xinsoft.common.BusinessType;
import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.SysConfigParam;
import com.xinsoft.entity.po.SysConfig;
import com.xinsoft.service.SysConfigService;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName SysConfigController
 * @Description 配置管理控制层
 * @Author lijie
 * @Date 2023年5月18日10:18:34
 * @Version 1.0
 */
@RestController
@RequestMapping("/system/config")
@Api(value = "SysConfigController", tags = {"配置管理控制层"})
public class SysConfigController {
	@Autowired
	private SysConfigService configService;


	/**
	 * @Author Li9527
	 * @Description 配置管理-新增
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@SysLog(title = "配置管理-新增", businessType = BusinessType.INSERT)
	@ApiOperation("新增")
	@PostMapping("/add")
	public Response<?> add(@RequestBody SysConfig config) {
		// 参数校验
		ValidatorUtils.validateEntity(config);
		SysConfig sysConfig = configService.getOne(new QueryWrapper<SysConfig>().eq("param_key",config.getParamKey()));
		if(sysConfig != null){
			return Response.fail("该项配置已存在,请不要重复添加");
		}
		return Response.succeed(configService.save(config));
	}

	/**
	 * @Author Li9527
	 * @Description 更新
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("更新")
	@SysLog(title = "配置管理-编辑", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	public Response<?> edit(@RequestBody SysConfig config) {
		// 参数校验
		ValidatorUtils.validateEntity(config);
		return Response.succeed(configService.updateById(config));
	}

	/**
	 * @Author Li9527
	 * @Description 获取详情
	 * @Date 2023/5/17 9:57
	 * @params   id
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("获取详情")
	@GetMapping("/detail")
	public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
		return Response.succeed(configService.getById(id));
	}

	/**
	 * @Author Li9527
	 * @Description 删除
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("删除")
	@SysLog(title = "配置管理-删除", businessType = BusinessType.DELETE)
	@PostMapping("/del")
	public Response<?> del(@RequestBody List<Integer> ids) {
		configService.removeByIds(ids);
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 通过param_key获取配置对象
	 * @Date 2023/5/17 9:57
	 * @params   id
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("通过param_key获取配置对象")
	@GetMapping("/getInfoByKey")
	public Response<?> getInfoByKey(@RequestParam(value = "paramKey", defaultValue = "", required = true) String paramKey) {
		return Response.succeed(configService.getOne(new QueryWrapper<SysConfig>().eq("param_key",paramKey)));
	}

	/**
	 * @Author Li9527
	 * @Description 配置管理分页
	 * @Date 2023/5/18 10:51
	 * @params   param
	 * @return com.xinsoft.common.Response<?>
	 **/
	@PostMapping("/pageList")
	public Response<?> pageList(@Valid @RequestBody SysConfigParam param) {
		return Response.succeed(configService.findPageListByParam(param));
	}
}
